package indi.kider.server.sms.service;

import indi.kider.common.constant.BillTypeCons;
import indi.kider.common.model.sms.*;
import indi.kider.server.sms.mapper.BillQueryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vankid.common.constant.DateTime;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by shixj on 2018/6/7.
 */
@Service
public class BillQueryServiceImpl implements BillQueryService {

    @Autowired
    private BillQueryMapper billQueryMapper;

    private List<RentBillForSms> getLimitOverdueBillList(Integer billType, Long len) {
        OverdueBillQuery query = new OverdueBillQuery();
        query.setBillType(billType);
        query.setLimitDate(getLimitTime(len));
        query.setLen(len);
        query.setLimitDateStr(DateTime.SHORT_DATE.format(query.getLimitDate()));
        return billQueryMapper.getOverdueBillList(query);
    }

    private List<RentTaskForSms> getLimitDueRentalBillList(Long len) {
        DueRentalBillQuery query = new DueRentalBillQuery();
        query.setLimitDate(getLimitTimePlus(len));
        query.setLen(len);
        query.setLimitDateStr(DateTime.SHORT_DATE.format(query.getLimitDate()));
        return billQueryMapper.getDueRentalBillList(query);
    }

    private List<RentForSms> getLimitDueRentList(Long len) {
        DueRentalBillQuery query = new DueRentalBillQuery();
        query.setLen(len);
        query.setLimitDate(getLimitTimePlus(len));
        query.setLimitDateStr(DateTime.SHORT_DATE.format(query.getLimitDate()));
        return billQueryMapper.getDueRentList(query);
    }

    private Date getLimitTime(Long len) {
        LocalDate limitDate = LocalDate.now().minusDays(len);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = limitDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }
    private Date getLimitTimePlus(Long len) {
        LocalDate limitDate = LocalDate.now().plusDays(len);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = limitDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }
    /**
     * 获取超期未支付账单列表 水费
     * 出账两天
     *
     * @return
     */
    @Override
    public List<RentBillForSms> getOverdueWaterBillList() {
        return getLimitOverdueBillList(BillTypeCons.WATER, 2L);
    }


    /**
     * 获取超期未支付账单列表 电费
     *
     * @return
     */
    @Override
    public List<RentBillForSms> getOverduePowerBillList() {
        return getLimitOverdueBillList(BillTypeCons.POWER, 2L);
    }

    /**
     * 获取超期未支付账单列表 物业
     *
     * @return
     */
    @Override
    public List<RentBillForSms> getOverdueEstateBillList() {
        return getLimitOverdueBillList(BillTypeCons.ESTATE, 2L);
    }

    /**
     * 获取超期未支付账单列表 网费
     *
     * @return
     */
    @Override
    public List<RentBillForSms> getOverdueNetBillList() {
        return getLimitOverdueBillList(BillTypeCons.NET, 2L);
    }

    /**
     * 获取超期未支付账单列表 房租
     * 1,2,3天连续提醒
     *
     * @return
     */
    @Override
    public List<RentBillForSms> getOverdueRentalBillList() {
        List<RentBillForSms> list = new ArrayList<>();
        for (long i = 1; i < 4; i++) {
            List<RentBillForSms> limitList = getLimitOverdueBillList(BillTypeCons.RENTAL, i);
            if (limitList != null && !limitList.isEmpty())
                list.addAll(limitList);
        }
        return list;
    }

    /**
     * 获取即将到期的房租
     *
     * @return
     */
    @Override
    public List<RentTaskForSms> getDueRentalBillList() {
        List<RentTaskForSms> list = new ArrayList<>();
        List<RentTaskForSms> limitList1 = getLimitDueRentalBillList(1L);
        if (limitList1 != null && !limitList1.isEmpty())
            list.addAll(limitList1);
        List<RentTaskForSms> limitList2 = getLimitDueRentalBillList(2L);
        if (limitList2 != null && !limitList2.isEmpty())
            list.addAll(limitList2);
        List<RentTaskForSms> limitList5 = getLimitDueRentalBillList(5L);
        if (limitList5 != null && !limitList5.isEmpty())
            list.addAll(limitList5);
        return list;
    }

    /**
     * 获取租期即将到期的
     *
     * @return
     */
    @Override
    public List<RentForSms> getDueRentList() {
        return getLimitDueRentList(5L);
    }
}
